﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms
{
    internal partial class SearchDropDownPanel : System.Windows.Forms.UserControl
    {

        private readonly SearchControl textBox;
        internal SearchDropDownPanel(SearchControl textBox)
        {
            InitializeComponent();
            this.listBox1.DrawMode = DrawMode.OwnerDrawVariable;
            this.textBox = textBox;
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem is SearchControl.FieldItem field)
            {
                this.textBox.AddValue(field.Field, field.Value);
                this.textBox.CloseDropDown();
            }

        }

        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.listBox1.SelectedItem is SearchControl.FieldItem field)
                {
                    this.textBox.AddValue(field.Field, field.Value);
                    this.textBox.CloseDropDown();
                }
            }
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            object item = this.listBox1.Items[e.Index];
            Rectangle rect = e.Bounds;
            Color backcolor = e.State == DrawItemState.Selected ? Color.FromArgb(124, 123, 173) : Color.White;
            Color forecolor = e.State == DrawItemState.Selected ? Color.White : Color.Black;
            int left = 0;
            Font font = new Font(this.Font.Name, this.Font.Size);

            using (SolidBrush brush = new SolidBrush(backcolor))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }
            if (item is Marmat.Forms.SearchControl.FieldItem field)
            {
                if (field.Field is SearchControl.Group group)
                {
                    TextRenderer.DrawText(e.Graphics, item.ToString(), font, rect, forecolor, backcolor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                }
                else
                {
                    string txt = "搜索";
                    Size size = TextRenderer.MeasureText(txt, font, rect.Size, TextFormatFlags.NoPadding);
                    TextRenderer.DrawText(e.Graphics, txt, font, rect, forecolor, backcolor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                    left += size.Width;

                    font = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
                    txt = field.Field.Caption ?? field.Field.Name;
                    rect = new Rectangle(left, e.Bounds.Y, e.Bounds.Width - left, e.Bounds.Height);
                    size = TextRenderer.MeasureText(e.Graphics, txt, font);
                    TextRenderer.DrawText(e.Graphics, txt, font, rect, forecolor, backcolor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                    left += size.Width;

                    font = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
                    txt = "为：";
                    rect = new Rectangle(left, e.Bounds.Y, e.Bounds.Width - left, e.Bounds.Height);
                    size = TextRenderer.MeasureText(txt, font, rect.Size, TextFormatFlags.NoPadding);
                    TextRenderer.DrawText(e.Graphics, txt, font, rect, forecolor, backcolor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                    left += size.Width;

                    font = new Font(this.Font.Name, this.Font.Size, FontStyle.Bold);
                    txt = field.Value;
                    rect = new Rectangle(left, e.Bounds.Y, e.Bounds.Width - left, e.Bounds.Height);
                    size = TextRenderer.MeasureText(txt, font, rect.Size, TextFormatFlags.NoPadding);
                    TextRenderer.DrawText(e.Graphics, txt, font, rect, forecolor, backcolor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                }
            }
        }

        private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 22;
        }
    }
}
